﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SafeScript.MetaModel
{
    public class BindingTypeDef : ITypeDef
    {
        public List<ITypeDef> ParameterTypes = new List<ITypeDef>();
        public ITypeDef ReturnType;
        public bool IsStatic = false;

        public BindingTypeDef()
        {
        }

        public bool IsEqualTo(ITypeDef typedef)
        {
            BindingTypeDef f = typedef as BindingTypeDef;
            if (f == null)
                return false;
            if (!this.ReturnType.IsEqualTo(f.ReturnType))
                return false;
            if (this.ParameterTypes.Count != f.ParameterTypes.Count)
                return false;
            for (var i = 0; i < this.ParameterTypes.Count; ++i)
                if (!this.ParameterTypes[i].IsEqualTo(f.ParameterTypes[i]))
                    return false;
            return true;
        }

        public static bool IsEqualTo(FunctionTypeDef t1, BindingTypeDef t2)
        {
            if (!t1.ReturnType.IsEqualTo(t2.ReturnType))
                return false;
            if (t1.ParameterTypes.Count != t2.ParameterTypes.Count)
                return false;
            for (var i = 0; i < t1.ParameterTypes.Count; ++i)
                if (!t1.ParameterTypes[i].IsEqualTo(t2.ParameterTypes[i]))
                    return false;
            return true;
        }

        public bool CanBeAssignedTo(ITypeDef typedef)
        {
            if (typedef is ObjectType)
                return true;
            return this.IsEqualTo(typedef);
        }

        public int GetInheritanceDepth(ITypeDef typedef)
        {
            if (typedef is Object)
                return 1;
            return this.IsEqualTo(typedef) ? 0 : -1;
        }

        public ITypeDef Instantiate(Dictionary<TemplateParameterType,ITypeDef> templateArguments)
        {
            throw new NotImplementedException();
        }
    }
}
